package com.app.housemanage.domain.vo;

import lombok.Data;

/**
 * 统一API响应结果
 */
@Data
public class ApiResult<T> {
    
    // 成功状态码
    public static final int SUCCESS_CODE = 200;
    public static final int CREATED_CODE = 201;
    
    // 客户端错误状态码
    public static final int BAD_REQUEST_CODE = 400;
    public static final int UNAUTHORIZED_CODE = 401;
    public static final int FORBIDDEN_CODE = 403;
    public static final int NOT_FOUND_CODE = 404;
    
    // 服务器错误状态码
    public static final int INTERNAL_SERVER_ERROR_CODE = 500;
    public static final int SERVICE_UNAVAILABLE_CODE = 503;
    
    /**
     * 状态码
     */
    private Integer code;
    
    /**
     * 消息
     */
    private String message;
    
    /**
     * 数据
     */
    private T data;
    
    /**
     * 时间戳
     */
    private Long timestamp;
    
    public ApiResult() {
        this.timestamp = System.currentTimeMillis();
    }
    
    public static <T> ApiResult<T> success(T data) {
        ApiResult<T> result = new ApiResult<>();
        result.code = 200;
        result.message = "success";
        result.data = data;
        return result;
    }
    
    public static <T> ApiResult<T> success(String message, T data) {
        ApiResult<T> result = new ApiResult<>();
        result.code = 200;
        result.message = message;
        result.data = data;
        return result;
    }
    
    public static <T> ApiResult<T> error(String message) {
        ApiResult<T> result = new ApiResult<>();
        result.code = 500;
        result.message = message;
        return result;
    }
    
    public static <T> ApiResult<T> error(Integer code, String message) {
        ApiResult<T> result = new ApiResult<>();
        result.code = code;
        result.message = message;
        return result;
    }
    
    public static <T> ApiResult<T> notFound() {
        ApiResult<T> result = new ApiResult<>();
        result.code = 404;
        result.message = "资源未找到";
        return result;
    }
    
    public static <T> ApiResult<T> badRequest(String message) {
        ApiResult<T> result = new ApiResult<>();
        result.code = BAD_REQUEST_CODE;
        result.message = message;
        return result;
    }
    
    public static <T> ApiResult<T> created(T data) {
        ApiResult<T> result = new ApiResult<>();
        result.code = CREATED_CODE;
        result.message = "创建成功";
        result.data = data;
        return result;
    }
    
    public static <T> ApiResult<T> unauthorized(String message) {
        ApiResult<T> result = new ApiResult<>();
        result.code = UNAUTHORIZED_CODE;
        result.message = message;
        return result;
    }
    
    public static <T> ApiResult<T> forbidden(String message) {
        ApiResult<T> result = new ApiResult<>();
        result.code = FORBIDDEN_CODE;
        result.message = message;
        return result;
    }
    
    public static <T> ApiResult<T> serviceUnavailable(String message) {
        ApiResult<T> result = new ApiResult<>();
        result.code = SERVICE_UNAVAILABLE_CODE;
        result.message = message;
        return result;
    }
    
    /**
     * 判断响应是否成功
     */
    public boolean isSuccess() {
        return code != null && code >= 200 && code < 300;
    }
    
    /**
     * 判断响应是否为客户端错误
     */
    public boolean isClientError() {
        return code != null && code >= 400 && code < 500;
    }
    
    /**
     * 判断响应是否为服务器错误
     */
    public boolean isServerError() {
        return code != null && code >= 500 && code < 600;
    }
}